﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Entity.Validation;
using HYI.SDBSERVER;
using eHCBaseClass;
using Sunny.UI;

namespace SHRBASE
{
    public partial class FMI_TS_GOOD_CLASS : SDBSERVER.FMSINGLE
    {
        //入口函数
        public static Int32 StartMe(string sMainPara, string sPara, string sUser, DBSERVER sDB)
        {
            StaticClass.Selectm_MainPara = sMainPara;
            StaticClass.SelectItemValue = sPara;
            StaticClass.SelectUser = sUser;
            StaticClass.myDB = sDB;
            return 0;
        }

        //DBSERVER myADO = new DBSERVER();
        DBCache myCache = new DBCache();
        private int iGridView = 0;
        private string sType = "V";

        public FMI_TS_GOOD_CLASS()
        {
            InitializeComponent();
        }

        //查询代码
        protected override void OnSys04()
        {
            string SQL = "select a.*,b.fclassstname fparentname"
              +"  from I_TS_GOOD_CLASS a"
              +"  left join I_TS_GOOD_CLASS b on a.fparentno = b.fclassstno"
              +"  where 1 = 1"
              +"  order by a.flevel";
            if (qifclassstno.Text != "")
            {
                SQL = SQL + " and ((a.fclassstno like '%" + qifclassstno.Text + "%') or (a.fclassstname like '%" + qifclassstno.Text + "%'))";
            }
            Qrytable = StaticClass.myDB.query(SQL);

            base.OnSys04();

            //表格列显示统一控制
            if (iGridView == 0)
            {
                myCache.doSetGridVIew(sMenu, sUser, uiDGView);
                iGridView = iGridView + 1;
            }
        }

        private void FMI_TS_GOOD_CLASS_Load(object sender, EventArgs e)
        {
            _INPUTTABLE = "Import_i_ts_good_class";
            sUser = StaticClass.SelectUser;
            string[] strArray = StaticClass.Selectm_MainPara.Split(new char[] { ';' });
            sMenu = strArray[1];

            //按钮控制
            sys07.Visible = false; sys07.Tag = "1";
            sys08.Visible = false; sys08.Tag = "1";
            sys09.Visible = false; sys09.Tag = "1";
            sys11.Visible = false; sys11.Tag = "1";

            //执行查询代码
            OnSys04();
        }

        private void uiBtn_Click(object sender, EventArgs e)
        {
            //弹窗选择物料分类
            uifparentno.Text = "";
            uifparentno.Tag = "";
            uiflevel.Text = "1";

            string DATA = doPOPWINDOS("物料分类查询"
                , "fclassstno,分类编号,121;fclassstname,分类名称,141"
                , "select fclassstno,fclassstname,fparentno,flevel from i_ts_good_class where flevel<3"
                , " order by fclassstno"
                ,0);
            Console.WriteLine("当前返回:" + DATA);

            if ((DATA is null) || (DATA.Length == 0))
            {
                return;
            }
            string[] strArray = DATA.Split(',');

            string sParent = uifparentno.Tag.ToString();
            if (sParent.Equals(strArray[0]))
            {
                UIMessageTip.ShowOk("不能选择自己");
                return;
            }

            int iLevel = int.Parse(strArray[3]);
            if (iLevel == 1)
            {
                uiflevel.Text = "2";
            }
            else
            {
                uiflevel.Text = "3";
            }
            uifparentno.Text = strArray[1];
            uifparentno.Tag = strArray[0];

        }

        //数据保存成功后执行操作
        private void doProc()
        {
            string sql = " update TABLEVERSION set VERSION=VERSION+1 where TABLENAME='I_TS_GOOD_CLASS'";
            StaticClass.myDB.doexec(sql);

            myCache.doCache();
        }

        //新增代码
        protected override void OnSys01()
        {
            uifclassstno.Text = "";
            uifclassstname.Text = "";
            uifparentno.Text = "";
            uifparentno.Tag = "";
            uiflevel.Text = "1";

            sType = "A";
            doLoadData();
            uifclassstno.ReadOnly = false;
            uifparentno.ReadOnly = true;
            uiBtn.Visible = true;

            base.OnSys01();
        }

        //编辑+浏览载入数据
        private void doLoadData()
        {
            //控制界面输入
            Boolean input = false;
            if ((sType.Equals("A")) || (sType.Equals("B")))
            {
                input = false;
            }
            else
            {
                input = true;
            }
            foreach (Control ctrl in uiPanelData.Controls)
            {
                if (ctrl is UITextBox)
                {
                    UITextBox m = (UITextBox)ctrl;
                    m.ReadOnly = input;
                }
            }

            //特殊按钮
            if ((sType.Equals("A")) || (sType.Equals("B")))
            {
                uiBtn.Visible = true;
            }
            else
            {
                uiBtn.Visible = false;
            }

            if (!sType.Equals("A"))
            {
                uifclassstno.Text = uiDGView["fclassstno", rowindex].Value.ToString();
                uifclassstname.Text = uiDGView["fclassstname", rowindex].Value.ToString();
                uifparentno.Text = uiDGView["fparentname", rowindex].Value.ToString();
                uifparentno.Tag = uiDGView["fparentno", rowindex].Value.ToString();
                uiflevel.Text = uiDGView["flevel", rowindex].Value.ToString();
            }

        }

        //编辑代码
        protected override void OnSys02()
        {
            Console.WriteLine("行:" + rowindex.ToString());
            if (rowindex == -1)
            {
                return;
            }
            sType = "B";

            doLoadData();

            uifclassstno.ReadOnly = true;
            uifparentno.ReadOnly = true;
            uiBtn.Visible = false;

            base.OnSys02();
        }

        //删除代码
        protected override void OnSys03()
        {
            Console.WriteLine("行:" + rowindex.ToString());
            if (rowindex == -1)
            {
                return;
            }

            if (this.ShowAskDialog("确定删除(" + uiDGView["fclassstname", rowindex].Value.ToString() + ")?"))
            {
                string sBillNo = uiDGView["fclassstno", rowindex].Value.ToString();
                string SQL = "exec proc_biz_dodelete 'i_ts_good_class','','" + sBillNo + "',2,'" + sUser + "'";
                DataTable myTable = StaticClass.myDB.query(SQL);

                string sMsg = myTable.Rows[0][0].ToString();
                if (sMsg.Equals("OK"))
                {
                    this.ShowInfoDialog("删除完毕");
                    myCache.doCache();
                    OnSys04();
                }
                else
                {
                    this.ShowInfoDialog(sMsg);
                }
            }
        }

        //浏览代码
        protected override void OnSys10()
        {
            Console.WriteLine("行:" + rowindex.ToString());
            if (rowindex == -1)
            {
                return;
            }
            sType = "V";

            doLoadData();

            base.OnSys10();

        }

        //数据为空检查
        private bool CheckData()
        {
            return CheckEmpty(uifclassstno, "请输入分类编号")
                   && CheckEmpty(uifclassstname, "请输入分类名称");
        }

        //保存代码
        protected override void OnSys08()
        {
            if (!CheckData())
            {
                return;
            }

            string sParent = uifparentno.Tag.ToString();
            if (sParent == "")
            {
                sParent = "";
                uiflevel.Text = "1";
            }

            if (sType == "A")
            {
                try
                {
                    using (XTBasEntities context = new XTBasEntities())
                    {
                        i_ts_good_class myData = context.i_ts_good_class.Find(uifclassstno.Text);
                        if (myData is null)
                        {
                            i_ts_good_class myData2 = new i_ts_good_class();
                            myData2.fclassstno = uifclassstno.Text;
                            myData2.fclassstname = uifclassstname.Text;
                            myData2.fparentno = sParent;
                            myData2.flevel = int.Parse(uiflevel.Text);
                            myData2.fclassstyle = 1;
                            myData2.feffday = 0;
                            myData2.fclassstno = uifclassstno.Text;
                            myData2.foperator = sUser;
                            myData2.fupddate = DateTime.Now;
                            context.i_ts_good_class.Add(myData2);
                            context.SaveChanges();
                            doProc();
                            UIMessageTip.ShowOk("添加成功");

                        }
                        else
                        {
                            //Console.WriteLine("添加成功");
                            UIMessageTip.ShowOk("编号已存在");
                            //Console.WriteLine("已存在");
                            return;
                        }

                    }
                }
                catch (DbEntityValidationException dbEx)
                {
                    Console.WriteLine("错误" + dbEx.Message);
                }
            }

            else if (sType == "B")
            {
                using (XTBasEntities context = new XTBasEntities())
                {
                    //查询
                    i_ts_good_class myts_goodclass = context.i_ts_good_class.Find(uifclassstno.Text);

                    myts_goodclass.fclassstname = uifclassstname.Text;
                    myts_goodclass.foperator = sUser;
                    myts_goodclass.fupddate = DateTime.Now;
                    int result = context.SaveChanges();
                    doProc();
                    UIMessageTip.ShowOk("修改成功");
                }
            }

            //刷新cache
            sType = "V";
            base.OnSys08();

            OnSys04();
        }

    }
}
